package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.VisitRecordMapper;
import com.j283_5.wisdom_park.mapper.VisitorsMapper;
import com.j283_5.wisdom_park.pojo.VisitRecord;
import com.j283_5.wisdom_park.pojo.Visitors;
import com.j283_5.wisdom_park.service.VisitRecordService;
import com.j283_5.wisdom_park.vo.visitor_management.VisitRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
@Transactional
public class VisitRecordServiceImpl extends ServiceImpl<VisitRecordMapper, VisitRecord> implements VisitRecordService {
    @Resource
    private VisitRecordMapper visitRecordMapper;
    @Resource
    private VisitorsMapper visitorsMapper;

    /**
     * 查询所有进出记录
     * @param page
     * @param visitor
     * @return
     */
    @Override
    public ResultData visitRecordList(Page<VisitRecord> page, Visitors visitor) {
//        QueryWrapper<Visitors> visitorsQueryWrapper = new QueryWrapper<>();
//        visitorsQueryWrapper.orderByDesc("entry_time");
//        if(visitor.getVisName() != null){
//            visitorsQueryWrapper.like("vis_name",visitor.getVisName());
//        }
//        if(visitor.getVisPhone() != null){
//            visitorsQueryWrapper.like("vis_phone",visitor.getVisPhone());
//        }
//        List<Visitors> visitorsList = visitorsMapper.selectList(visitorsQueryWrapper);
//
//        Page<VisitRecord> visitRecordPage=null;
//        if(visitorsList.size() > 0){
//            ArrayList<VisitRecordVo> voArrayList = new ArrayList<>();
//            for(Visitors visitors:visitorsList){
//                QueryWrapper<VisitRecord> visitRecordQueryWrapper = new QueryWrapper<>();
//                Integer thisVisId = visitors.getVisId();
//                String thisVisName = visitors.getVisName();
//                String thisVisPhone = visitors.getVisPhone();
//                visitRecordQueryWrapper.eq("vis_id",visitors.getVisId());
//                visitRecordPage = visitRecordMapper.selectPage(page,visitRecordQueryWrapper);
//                List<VisitRecord> visitRecordList = visitRecordPage.getRecords();
//                if(visitRecordList.size() > 0){
//                    for(VisitRecord visitRecord : visitRecordList){
//                        VisitRecordVo visitRecordVo = new VisitRecordVo();
//                        BeanUtils.copyProperties(visitRecord,visitRecordVo);
//                        if(visitRecord.getVireDirection() == 0){
//                            visitRecordVo.setVireDirection("入场");
//                        }else{
//                            visitRecordVo.setVireDirection("出场");
//                        }
//                        visitRecordVo.setVisId(thisVisId);
//                        visitRecordVo.setVisName(thisVisName);
//                        visitRecordVo.setVisPhone(thisVisPhone);
//                        voArrayList.add(visitRecordVo);
//                    }
//                }
//            }
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("data",voArrayList);
//            map.put("count",visitRecordPage.getTotal());
//            return ResultData.ok().data(map);
//        }

        QueryWrapper<Visitors> visitorsQueryWrapper = new QueryWrapper<>();
        visitorsQueryWrapper.like("vis_name",visitor.getVisName());
        visitorsQueryWrapper.like("vis_phone",visitor.getVisPhone());
        List<Visitors> visitorsList = visitorsMapper.selectList(visitorsQueryWrapper);
        ArrayList<Integer> visIdList = new ArrayList<>();
        for(Visitors visitors : visitorsList){
            visIdList.add(visitors.getVisId());
        }
        QueryWrapper<VisitRecord> visitRecordQueryWrapper = new QueryWrapper<>();
        visitRecordQueryWrapper.eq("vire_state",0);
        visitRecordQueryWrapper.orderByAsc("vire_endtime");
        if(visIdList.size() > 0){
            visitRecordQueryWrapper.in("vis_id",visIdList);
        }
        Page<VisitRecord> visitRecordPage = visitRecordMapper.selectPage(page, visitRecordQueryWrapper);
        List<VisitRecord> records = visitRecordPage.getRecords();
        if(records.size() > 0 ){
            ArrayList<VisitRecordVo> voArrayList = new ArrayList<>();
            for(VisitRecord visitRecord : records){
                VisitRecordVo visitRecordVo = new VisitRecordVo();
                Visitors visitors = visitorsMapper.selectById(visitRecord.getVisId());
                if(visitors != null) {
                    BeanUtils.copyProperties(visitors, visitRecordVo);
                }
                visitRecordVo.setVireType(visitRecord.getVireType());
                visitRecordVo.setVireStarttime(visitRecord.getVireStarttime());
                visitRecordVo.setVireDirection(visitRecord.getVireDirection()==0?"入场":"出场");
                visitRecordVo.setVireEndtime(visitRecord.getVireEndtime());
                visitRecordVo.setVireId(visitRecord.getVireId());
                voArrayList.add(visitRecordVo);
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("data",voArrayList);
            map.put("count",visitRecordPage.getTotal());
            return ResultData.ok().data(map);
        }

        return ResultData.error().message("未查询到该访客信息！");
    }

    @Override
    public ResultData deleteVisitRecord(ArrayList<Integer> vireIdList) {
        UpdateWrapper<VisitRecord> visitRecordUpdateWrapper = new UpdateWrapper<>();
        visitRecordUpdateWrapper.set("vire_state",1);
        visitRecordUpdateWrapper.in("vire_id",vireIdList);
        int i = visitRecordMapper.update(null, visitRecordUpdateWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功！");
        }
        return ResultData.error().message("删除失败！");
    }
}
